﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Web.Models;
using Batman.Cms.Core.Controllers;
using Batman.Jianbei.Forum.Controllers.Dicts.Filters;
using Batman.Jianbei.Forum.Controllers.Dicts.PartitionInputs;
using Batman.Jianbei.Forum.Core.Domains.Dicts;
using Batman.Jianbei.Forum.Services.Dicts;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Batman.Jianbei.Forum.Controllers.Dicts
{
    public class PartitionController : BatmanController<Partition>
    {
        public PartitioManager PartitioManager { get; set; }

        public IRepository<Partition, string> Rep { get; set; }

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public async Task<ActionResult> Page(PagedResultRequestDto page, PartitionFilter filter)
        {
            var result = await Reader.GetPageAsync(page, query =>
            {
                if (false == filter.ShowDeleted)
                {
                    query = query.Where(t => !t.IsDeleted);
                }
                if (!string.IsNullOrEmpty(filter.NameOrNormalizedName))
                {
                    query = query.Where(t => t.Name.Contains(filter.NameOrNormalizedName) || t.NormalizedName.Contains(filter.NameOrNormalizedName));
                }

                query = query.OrderByDescending(t => t.Sort).ThenByDescending(t => t.CreateAt).AsQueryable();
                return query;
            });

            return Json(result);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ActionResult> Create(PartitionCreateInput model)
        {
            var result = await PartitioManager.CreateAsync(model);
            return Json(result);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ActionResult> Update(PartitionUpdateInput model)
        {
            var result = await PartitioManager.UpdateAsync(model);
            return Json(result);
        }

        /// <summary>
        /// 回收分区
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult Recover(List<string> ids)
        {
            var result = this.BaseManager.Excute(rep =>
           {
               foreach (var id in ids)
               {
                   rep.Update(id, t => t.IsDeleted = true);
               }

               return new AjaxResponse();
           });
            return Json(result);
        }

        /// <summary>
        /// 下拉框参数
        /// </summary>
        /// <returns></returns>
        public ActionResult Selection()
        {
            var result = Reader.Query(repository =>
            {
                var query = repository.Where(t => !t.IsDeleted);
                query = query.OrderByDescending(t => t.Sort).ThenByDescending(t => t.CreateAt);

                var newQuery = query.Select(t => new NameValueDto(t.Name, t.Id)).AsQueryable();

                return newQuery;
            });

            return Json(result);
        }
    }
}
